package com.itheima.web.task;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.db.UserFreeze;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.db.UserThaw;
import com.itheima.service.db.AdminService;
import com.itheima.service.db.UserFreezeService;
import com.itheima.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class FreezeTask {
    @Reference
    private UserFreezeService userFreezeService;
    @Reference
    private UserInfoService userInfoService;

    /**
     * 任务执行 检测当前时间戳是否大于用户冻结表中的时间戳
     * 如果大于则解封  每分钟执行一次
     */
    @Scheduled(cron = "0/10 * * * * ? ")
    public void addAnalysis() {
        //System.out.println("检测开始...");
        List<UserFreeze> freezeList = userFreezeService.findAll();
        if (CollectionUtil.isNotEmpty(freezeList)) {
            for (UserFreeze userFreeze : freezeList) {
                UserInfo userInfo = userInfoService.findById(userFreeze.getUserId());
                if (System.currentTimeMillis() > userFreeze.getTime()) {
                    userInfo.setUserStatus("1");
                    UserThaw userThaw =new UserThaw();
                    userThaw.setUserId(userInfo.getId());
                    userThaw.setReasonsForThawing("自动解封");
                    userFreezeService.freezeUnseal(userInfo,userThaw);
                }
            }
        }
    }
}
